
/*
 * 文件名：Yemian.java.
 * 版权：Copyright 2014-2017 上海酷方网络科技有限公司 All Rights Reserved.
 * 描述：
 * 修改人: Kevin
 * 修改时间：2017年5月25日
 * 修改单号：
 * 修改内容：
 */

package com.ghl.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.skf.mokao.mapper.ErshouMapper;
import com.skf.mokao.pojo.Ershou;


/**
 * 
 * 
 * @author Kevin
 * @version 2017年5月25日
 * @see
 * @since 
 *
 */

@Repository
public class YemianImp implements Yemian{
	@Autowired
	private ErshouMapper ershouMapper;
	
	public  Map<String, String> getCookies() throws IOException {
		File cookieFile = new File("E:/my_workspace/fetcher/cookies.txt");
		if (!cookieFile.exists()) {
			return null;
		}
		FileInputStream in = new FileInputStream(cookieFile);
		byte[] data = new byte[1024];

		in.read(data);
		String str = new String(data);
		String[] map = str.split(";");
		Map<String, String> cookieMap = new HashMap<String, String>();
		for (String m : map) {
			String[] c = m.split("=");
			if (c.length > 1) {
				cookieMap.put(c[0], m.substring(m.indexOf("=") + 1));
			} else {
				cookieMap.put(c[0], "");
			}
		}
		in.close();
		return cookieMap;
	}
	
	
	public void zhuaqu(BlockingQueue<Integer> queue, Object obj) throws ParserConfigurationException, XPathExpressionException {
		try {
			Map<String, String> cookies = getCookies();

			while(true) {
//				try {
//					Thread.sleep(400);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
				Integer take = 1;
				try {
					take = queue.take();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				Response response = Jsoup
						.connect("https://wh.lianjia.com/ershoufang/pg" + take+"/")
						.cookies(cookies).method(Method.GET).execute();
				String body = response.body();
				
				HtmlCleaner hc = new HtmlCleaner();
				TagNode tn = hc.clean(body);
				
				Document dom = new DomSerializer(new CleanerProperties()).createDOM(tn);
				XPath xPath = XPathFactory.newInstance().newXPath();
				Object result;
				for(int k=1;k<=30;k++) {
					List<String> list2 = new ArrayList<>();
					
					String[] exps = {"/html/body/div[4]/div[1]/ul/li["+k+"]/div[1]/div[1]/a","/html/body/div[4]/div[1]/ul/li["+k+"]/div[1]/div[2]/div/a",
							"/html/body/div[4]/div[1]/ul/li["+k+"]/div[1]/div[2]/div/text()","/html/body/div[4]/div[1]/ul/li["+k+"]/div[1]/div[3]/div/text()",
							"/html/body/div[4]/div[1]/ul/li["+k+"]/div[1]/div[6]/div[1]/span","/html/body/div[4]/div[1]/ul/li["+k+"]/div[1]/div[6]/div[2]/span","/html/body/div[4]/div[1]/ul/li["+k+"]/div[1]/div[1]/a"};
					//System.out.println("*******************************************************");
					for(int t=0;t<7;t++) {
						
						result = xPath.evaluate(exps[t], dom, XPathConstants.NODESET);
						
						if (result instanceof NodeList) {
							NodeList nodeList = (NodeList) result;
							//System.out.println(nodeList.getLength());
							for (int j = 0; j < nodeList.getLength(); j++) {
								Node node = nodeList.item(j);
								//System.out.println(node.getAttributes().getNamedItem("href").getNodeValue());
								list2.add(node.getNodeValue() == null ? node.getTextContent() : node.getNodeValue());
								//System.out.println(node.getNodeValue() == null ? node.getTextContent() : node.getNodeValue());
								if(t==6) {
									Response response2 = Jsoup
											.connect(node.getAttributes().getNamedItem("href").getNodeValue())
											.cookies(cookies).method(Method.GET).execute();
									//System.out.println(node.getAttributes().getNamedItem("href").getNodeValue());
									String body2 = response2.body();
									
									HtmlCleaner hc2 = new HtmlCleaner();
									TagNode tn2 = hc2.clean(body2);
									
									Document dom2 = new DomSerializer(new CleanerProperties()).createDOM(tn2);
									Object result2;
									String[] expSub = {"/html/body/div[5]/div[2]/div[4]/div[2]/span[2]/a[1]","/html/body/div[5]/div[2]/div[4]/div[1]/a[1]","/html/body/div[5]/div[2]/div[4]/div[2]/span[2]/a[2]",
											"/html/body/div[5]/div[2]/div[4]/div[2]/span[2]/text()[2]","/html/body/div[5]/div[2]/div[4]/div[2]/a","//*[@id='introduction']/div/div/div[2]/div[2]/ul/li[6]/text()"};
									for(int u=0;u<6;u++) {
										result2 = xPath.evaluate(expSub[u], dom2, XPathConstants.NODESET);
										if(result2 instanceof NodeList) {
											NodeList nodeList2 = (NodeList) result2;
											//System.out.println(nodeList2.getLength());
											for(int jj=0;jj<nodeList2.getLength();jj++) {
												Node node2 = nodeList2.item(jj);
												list2.add(node2.getNodeValue() == null ? node2.getTextContent() : node2.getNodeValue());
												//System.out.println(node2.getNodeValue() == null ? node2.getTextContent() : node2.getNodeValue());
											}
										}
									}
								}
							}
						}
					}
					insert(list2, obj);
					//list1.add(list2);
				}
				//list.add(list1);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static List<List<List<String>>> getList() {
		List<List<List<String>>> list = new ArrayList<>();
		String[][][] sss = {{{"中北路安顺家园精装修全南三居满五唯一","安顺家园","|3室2厅|129.52平米|南|精装","中楼层(共18层)2008年建塔楼-","380","单价29340元/平米","中北路安顺家园精装修全南三居满五唯一","武昌","安顺家园","楚河汉街"," 一环内","近4号线楚河汉街站","非共有"},{"大华南湖公园世家二期精装三居室南北通透业主诚售","大华南湖公园世家二期","|3室2厅|132.7平米|南北|精装","低楼层(共11层)2009年建板楼-","270","单价20347元/平米","大华南湖公园世家二期精装三居室南北通透业主诚售","洪山","大华南湖公园世家二期","新南湖"," 二至三环","","非共有"},{"仁和路金鹤园中装两房，采光好，配套成熟。","金鹤园小区","|2室2厅|89.85平米|南北|简装","低楼层(共7层)2002年建板楼-","140","单价15582元/平米","仁和路金鹤园中装两房，采光好，配套成熟。","洪山","金鹤园小区","青山"," 二至三环","","共有"}},{{"硚口金利屋2室2厅248万,硚口金利屋","|2室2厅|93.84平米|南|其他|有电梯","中楼层(共30层)2009年建板塔结合-","248","单价26428元/平米","硚口金利屋2室2厅248万","硚口","硚口金利屋","集贤","二至三环","近6号线武胜路站","共有"},{"街道口南北通透两房双阳台中间楼层两证满两年,省建委宿舍","|2室1厅|72.52平米|南北|简装","中楼层(共6层)板楼-","120","单价16548元/平米","街道口南北通透两房双阳台中间楼层两证满两年","洪山","省建委宿舍","街道口","","","共有"},{"亚安花园4室2厅320万","亚安花园","|4室2厅|162.48平米|南|精装","中楼层(共18层)板楼-","320","单价19695元/平米","亚安花园4室2厅320万","江岸","亚安花园","黄埔永清","","","非共有"}}};
		for(String[][] ss:sss) {
			List<List<String>> list1 = new ArrayList<List<String>>();
			for(String[] s:ss) {
				List<String> list2 = new ArrayList<>();
				for(String a:s) {
					list2.add(a);
				}
				list1.add(list2);
			}
			list.add(list1);
		}
		return list;
	}
	
	
	public synchronized void insert(List<String> ss, Object obj) {
		
		try {
			Ershou item = new Ershou();
			item.setTitle(ss.get(0));
			item.setXiaoqumin(ss.get(1));
			String string = ss.get(2);
			String[] shuxin = string.split("\\|");
			
			String huxin  = shuxin[1].trim();
			if(huxin.contains("室")) {
				
				item.setHuxin(Integer.parseInt(huxin.substring(0,1)));
			} else {
				item.setHuxin(4);
			}
			
			
			String mianji = shuxin[2].trim();
			item.setMianji(Float.parseFloat(mianji.substring(0,mianji.length()-2)));
			
			String chaoxiang = shuxin[3].trim();
			
			if(chaoxiang.contains("南")) {
				item.setChaoxian(1);
			} else {
				item.setChaoxian(0);
			}
			
			String jinzhuan = shuxin[4].trim();
			
			
			if(jinzhuan.contains("精装")) {
				item.setZhuanxiu(1);
			} else if(jinzhuan.contains("简装")) {
				item.setZhuanxiu(2);
			} else {
				item.setZhuanxiu(3);
			}
			
			item.setDianti(0);
			if(shuxin.length==6) {
				String dianti = shuxin[5];
				if(dianti.contains("有电梯")) {
					item.setDianti(1);
				} else {
					item.setDianti(0);
				}
			}
			
			String loucenshijian = ss.get(3);
			String regLoucen = "(低)|(中)|(高)";
			Pattern pLou = Pattern.compile(regLoucen);
			Matcher matcherLou = pLou.matcher(loucenshijian);
			if(matcherLou.find()) {
				if(matcherLou.group().equals("低")) {
					item.setLoucen(1);
				} else if(matcherLou.group().equals("中")) {
					item.setLoucen(2);
				} else {
					item.setLoucen(3);
				}
			}
			
			String regShijian = "[1-9][0-9][0-9][0-9]";
			Pattern pShijian = Pattern.compile(regShijian);
			Matcher matcherShijian = pShijian.matcher(loucenshijian);
			if(matcherShijian.find()) {
				item.setShijian(2017-Integer.parseInt(matcherShijian.group(0)));
			}
			
			String zj = ss.get(4);
			if(zj.contains(".")) {
				item.setZongjia((int)Float.parseFloat(zj));
			} else {
				item.setZongjia(Integer.parseInt(zj));
			}
			
			
			String danjia = ss.get(5);
			Pattern pDanjia = Pattern.compile("(\\d+)");
			Matcher mDanjia = pDanjia.matcher(danjia);
			if(mDanjia.find()) {
				item.setDanjia(Integer.parseInt(mDanjia.group()));
			}
			
			
			
			item.setDiqu(ss.get(7));
			
			String huanxian = ss.get(10).trim();
			if(huanxian.contains("一环内")) {
				item.setHuanxian(1);
			} else if(huanxian.contains("一至二环")) {
				item.setHuanxian(2);
			} else if(huanxian.contains("二至三环")) {
				item.setHuanxian(3);
			} else if(huanxian.contains("三至四环")) {
				item.setHuanxian(4);
			} else if(huanxian.isEmpty()|| huanxian.equals("") ||huanxian==""){
				item.setHuanxian(null);
			}
			
			item.setDitie(ss.get(11).isEmpty()?0:1);
			
			String chanquan = ss.get(12);
			if(chanquan.contains("非共有")) {
				item.setChanquan(1);
			} else {
				item.setChanquan(0);
			}
			synchronized (obj) {
				ershouMapper.insert(item);
				System.out.println(item.getTitle());
			}
		} catch (Exception e) {
			
		}

	}
}
